<template>
  <view class="work-info-form">
    <!-- 使用通用展示组件 -->
    <DetailDisplayList
      :config="config"
      :list="workList"
      :loading="loading"
      @add="handleAddWork"
      @edit="handleEditWork"
      @delete="handleDeleteWork"
    />

    <!-- 弹窗已移动到父页面顶层 -->
  </view>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import SysHrInterviewRequestBillApi from "../api/index";
import type {
  IWorkResponse,
  IWorkRequest,
  ISubTableSearchRequest,
} from "../api/interface";
import DetailDisplayList from "./DetailDisplayList.vue";
import { workDisplayConfig } from "./detailDisplayConfig";

const api = SysHrInterviewRequestBillApi();

// Props
interface Props {
  personId?: string;
}

const props = withDefaults(defineProps<Props>(), {
  personId: "",
});

// 数据
const loading = ref(false);
const workList = ref<IWorkResponse[]>([]);

// Emits
const emit = defineEmits<{
  add: [];
  edit: [item: IWorkResponse];
}>();

// 详情展示配置
const config = ref(workDisplayConfig);

// 移除了工作经历提交处理，因为弹框已移到父组件

// 加载工作经历列表
const loadWorkList = async () => {
  if (!props.personId) {
    return;
  }

  loading.value = true;

  try {
    const requestData: ISubTableSearchRequest = {
      Limit: 50,
      Page: 1,
      PersonId: props.personId,
      SortInfo: null,
    };

    const res = await api.getWorkList(requestData);

    if (res && res.Data && Array.isArray(res.Data)) {
      workList.value = res.Data;
    } else {
      workList.value = [];
    }
  } catch (error) {
    console.error("工作信息组件：获取工作经历异常:", error);
    workList.value = [];
    uni.showToast({
      title: "获取工作经历失败",
      icon: "none",
    });
  } finally {
    loading.value = false;
  }
};

// 处理新增工作信息
const handleAddWork = () => {
  if (!props.personId) {
    uni.showToast({
      title: "缺少人员信息",
      icon: "none",
    });
    return;
  }

  // 发出事件给父组件处理
  emit("add");
};

// 处理编辑工作信息
const handleEditWork = (item: IWorkResponse) => {
  if (!props.personId) {
    uni.showToast({
      title: "缺少人员信息",
      icon: "none",
    });
    return;
  }

  // 发出事件给父组件处理
  emit("edit", item);
};

// 处理删除工作信息
const handleDeleteWork = (item: IWorkResponse) => {
  uni.showModal({
    title: "确认删除",
    content: `确定要删除在"${item.Company || "未知公司"}"的工作经历吗？`,
    confirmText: "删除",
    confirmColor: "#ef4444",
    cancelText: "取消",
    success: async (res) => {
      if (res.confirm) {
        await deleteWork(item.ID);
      }
    },
  });
};

// 删除工作信息
const deleteWork = async (id: string) => {
  try {
    uni.showLoading({
      title: "删除中...",
    });

    await api.deleteWork([id]);

    uni.hideLoading();
    uni.showToast({
      title: "删除成功",
      icon: "success",
    });

    // 刷新列表
    loadWorkList();
  } catch (error) {
    uni.hideLoading();
    console.error("删除工作经历失败:", error);
    uni.showToast({
      title: "删除失败，请重试",
      icon: "none",
    });
  }
};

// 移除了处理新增成功的方法，因为弹框已移到父组件

// 监听personId变化，加载工作经历
watch(
  () => props.personId,
  (newId) => {
    if (newId) {
      loadWorkList();
    }
  },
  { immediate: true },
);

// 暴露方法供父组件调用
defineExpose({
  loadWorkList,
});
</script>

<!-- 工作经历组件不需要自定义样式，完全使用通用组件 -->
